﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ultima;

namespace SpellCaster3.Forms.Controls
{
    public partial class HueControl : UserControl
    {
        static HueControl()
        {
            for (int x = 0; x < Gradient.Width; ++x)
                for (int y = 0; y < Gradient.Height; ++y)
                    Gradient.SetPixel(x, y, Color.FromArgb(x, x, x));
        }

        public static readonly Bitmap Gradient = new Bitmap(255, 20);

        public void Reset()
        {
            HueIndex = 1;
        }
        
        public HueControl(int hueIndex)
        {
            InitializeComponent();

            if (HueIndex == hueIndex)
                Render();
            else
                HueIndex = hueIndex;
        }

        public HueControl()
            : this(1)
        {
        }

        private bool _Highlighted = false;
        public bool Highlighted
        {
            get { return _Highlighted; }
            private set
            {
                if (value != _Highlighted)
                {
                    _Highlighted = value;
                    BackColor = (value ? Color.Green : Color.White);
                }
            }
        }

        private int _HueIndex = 1;
        public int HueIndex
        {
            get { return _HueIndex; }
            set
            {
                //if (value < 1 || value > Hues.List.Length) throw new ArgumentOutOfRangeException("value", value.ToString() + " is not smaller than 1 and greater than " + Hues.List.Length);
                //Enabled = Enabled ? !(value < 1 || value > Hues.List.Length) : Enabled;
                if (value != _HueIndex)
                {
                    _HueIndex = value;
                    Render();
                }
            }
        }

        private void Render()
        {
            indexLabel.Text = HueIndex.ToString();
            if (imagePictureBox.Image != null) imagePictureBox.Image.Dispose();
            if (Enabled)
            {
                imagePictureBox.Image = Gradient.Clone() as Image;
                Hues.GetHue(HueIndex - 1).ApplyTo(imagePictureBox.Image as Bitmap, false);
            }
            else
            {
                Bitmap bmp = new Bitmap(SpellCaster3.Properties.Resources.BaseIcon.Width, SpellCaster3.Properties.Resources.BaseIcon.Height);
                using (Graphics g = Graphics.FromImage(bmp))
                    g.FillRectangle(SystemBrushes.FromSystemColor(Control.DefaultBackColor), 0, 0, bmp.Width, bmp.Height);
                imagePictureBox.Image = bmp;
            }
            imagePictureBox.Refresh();
        }

        private void HueControl_Enter(object sender, EventArgs e)
        {
            if (Enabled) Highlighted = true;
        }

        private void HueControl_Leave(object sender, EventArgs e)
        {
            if (Enabled) Highlighted = false;
        }

        private void HueControl_Click(object sender, EventArgs e)
        {
            //Al click su una parte non dell'usercontrol (l'immagine o la label), triggera un click sul control (in modo che sia trackabile)
            OnClick(e);
        }

        private void HueControl_EnabledChanged(object sender, EventArgs e)
        {
            Highlighted = Enabled;
            BackColor = (Enabled ? Color.White : Color.FromKnownColor(KnownColor.Control));
            Render();
        }
    }
}
